﻿using System;
using System.IO;

namespace PPI.General
{
    public class WebUploaderHelper
    {
        private WebUploader m_ServiceUploader;
        private string m_Foldername;
        private FileInfo m_FileSource;
        private string m_UrlWebpage;

        public string UrlWebpage
        {
            set { m_UrlWebpage = value; }
        }

        //
        //string K_URL = "http://www.dev-poleposition.com/Deploy/Website2.1/Upload.aspx?F=1000_24&C=" + reader.ConectionMode;
        //string K_FILE = "C://PolePosition2.1//Outbox//GPS//20080811091547_-1968574158//Images//285.jpeg";
        //string url = "http://localhost:4157/Upload.aspx?F=1000_24&C=" + reader.ConectionMode;
        //
        public WebUploaderHelper(string url)
        {
            m_UrlWebpage = url;
            m_ServiceUploader = new WebUploader();
        }

        public void Upload(string folder,string fileName, FileInfo fileSource, string databaseType)
        {
            string url = "";
            if (String.IsNullOrEmpty(m_UrlWebpage))
                return;
            else
            {
                ConfigReader reader = ConfigReader.GetInstance();
                url = m_UrlWebpage+ "?T=" + databaseType + "&F=" + folder + "&FI=" + fileName + "&C=" + reader.ConectionMode;
            }
            m_ServiceUploader.UploadFilesToRemoteUrl(fileSource.FullName, url);

        }

        public int FileSizeOnServer()
        {
            int ContentLength;
            System.Net.WebResponse resp = null;

            try
            {
                System.Net.WebRequest req = System.Net.WebRequest.Create(m_UrlWebpage);
                req.Method = "HEAD";
                resp = req.GetResponse();

                int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength);
            }
            catch
            {
                ContentLength = 0;
            }
            finally
            {
                if (resp != null)
                    resp.Close();
            }

            return ContentLength;
        }
    }
}
